package com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.service;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceAddDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceListEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper.DeviceListMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceListService {

    @Autowired
    DeviceListMapperImpl deviceListMapper;

    private static final Logger logger = LoggerFactory.getLogger(DeviceListService.class);

    // 列表和分页查询
    public DataResult<DeviceListEntity> SelectDeviceList(String tenant_id, String deviceName, String deviceCode, String deviceinfo,
                                                   String status, String type, String id) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(1, "操作成功!");
        List<DeviceListEntity> deviceListEntities = deviceListMapper.GetDeviceList(tenant_id, deviceName, deviceCode, deviceinfo, status, type, id, 0, 999);
        dataResult.setDataListEx(deviceListEntities);
        return dataResult;
    }

    /**
     * 根据条件查询分页的设备列表
     *
     * @param tenant_id  tenantId
     * @param deviceName 设备名称
     * @param deviceCode 设备编码
     * @param status     设备状态
     * @param page       页面
     * @param pagesize   页面大小
     * @return 返回列表
     */
    public DataResult<DeviceListEntity> getDeviceListPage(String tenant_id, String deviceName, String deviceCode, String deviceinfo,
                                                      String status, String type,
                                                      Integer page, Integer pagesize) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "设备列表信息查询失败!");
        Integer startNumber = (page - 1) * pagesize;
        try {
            List<DeviceListEntity> deviceListEntity = new ArrayList<>();
            int deviceListCount = 0;
            deviceListCount = deviceListMapper.GetDeviceListCount(tenant_id, deviceName, deviceCode, status, type);
            deviceListEntity = deviceListMapper.GetDeviceList(tenant_id, deviceName, deviceCode, deviceinfo, status, type,"0",
                    startNumber, pagesize);
            dataResult.setStatus(1);
            dataResult.setDataList(deviceListEntity);
            dataResult.setTotalSize(deviceListCount);
            dataResult.setMessage("查询成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据ID 查询设备信息
     *
     * @param tenantid tenantid
     * @param id       设备id
     * @return 返回设备信息
     */
    public DataResult<DeviceListEntity> getDeviceById(String tenantid, String id) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "获取信息失败");
        DeviceListEntity deviceListEntity;
        try {
            if (id != null && id.length() > 0) {
                deviceListEntity = deviceListMapper.GetDeviceById(tenantid, id);
                dataResult.setData(deviceListEntity);
                dataResult.setStatus(1);
                dataResult.setMessage("查询成功");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 添加设备信息
     *
     * @param tenantid     tenantid
     * @param deviceAddDto 添加设备列表
     * @return 返回格式化请求信息
     */
    public DataResult AddDevice(String tenantid, List<DeviceAddDto> deviceAddDto) {
        DataResult dataResult = new DataResult(0, "添加失败");
        List<DeviceListEntity> deviceListEntities = new ArrayList<>();
        try {
            if (deviceAddDto != null && deviceAddDto.size() > 0) {
                for (DeviceAddDto item : deviceAddDto
                ) {
                    DeviceListEntity deviceList = new DeviceListEntity();
                    deviceList.setId(Long.parseLong(MyTool.getRandomId()));
                    deviceList.setType(item.getType());
                    deviceList.setDevicename(item.getDevicename());
                    deviceList.setDevicecode(item.getDevicecode());
                    deviceList.setDeviceinfo(item.getDeviceinfo());
                    deviceList.setStatus(item.getStatus());
                    deviceList.setCreate_time(MyTool.GenerateTime());
                    deviceList.setDr(0);
                    deviceList.setTenant_id(tenantid);
                    deviceList.setOrgId(item.getOrgId());
                    deviceList.setVersion(item.getVersion());
                    deviceList.setApplicationId(item.getApplicationId());
                    deviceList.setAppCode(item.getAppCode());
                    deviceList.setIpAddress(item.getIpAddress());
                    deviceList.setYtenant_id(item.getYtenant_id());
                    deviceListEntities.add(deviceList);
                }
                int count = deviceListMapper.InsertDevice(deviceListEntities);
                String Message = "成功添加【 " + count + " 】条数据";
                dataResult.setMessage(Message);
                dataResult.setStatus(1);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据ID 查询设备信息
     *
     * @param id 设备id
     * @return 返回设备信息
     */
    public DataResult DeleteDevice(String id) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "删除失败");
        try {
            if (id != null && id.length() > 0) {
                int count = deviceListMapper.DeleteDevice(id);
                if (count != 0) {
                    dataResult.setStatus(1);
                    dataResult.setMessage("删除成功");
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 更新device 信息
     *
     * @param id         id
     * @param deviceList device
     * @return 返回结果
     */
    public DataResult ModifyDevice(String id, DeviceListEntity deviceList) {
        DataResult dataResult = new DataResult(0, "修改失败");
        if (id == null || id.length() <= 0) return dataResult;
        if (deviceList == null) return dataResult;
        try {
            if (deviceList.getId() == 0) deviceList.setId(Long.parseLong(id));
            int result = deviceListMapper.ModifyDevice(deviceList);
            if (result == 1) {
                dataResult.setStatus(1);
                dataResult.setMessage("修改成功");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据IDs获取排除在外的设备
     *
     * @param tenant_id tenantId
     * @param IDs       ids 合集
     * @return 返回列表
     */
    public DataResult<DeviceListEntity> getDeviceList(String tenant_id, String IDs) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "设备列表信息查询失败!");
        List<String> eliminatedIds = new ArrayList<>();
        if (IDs != null && IDs.length() > 0) {
            eliminatedIds = MyTool.StringRemoveLastSymbolToList(IDs, ",");
        }
        try {
            List<DeviceListEntity> deviceListEntity = new ArrayList<>();
            deviceListEntity = deviceListMapper.GetDeviceListExcludeIds(tenant_id, eliminatedIds);
            dataResult.setStatus(1);
            dataResult.setDataList(deviceListEntity);
            dataResult.setMessage("查询成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    public DeviceListEntity SelectEntityByKey(String tenantId, String key) {
        return deviceListMapper.selectEntityByKey(tenantId, key);
    }

    public void InsertEntity(int type, String devicetag, String tenantId) {
        List<DeviceListEntity> deviceList = new ArrayList<>();
        DeviceListEntity deviceEntity = new DeviceListEntity();
        deviceEntity.setId(Long.parseLong(MyTool.getRandomId()));
        deviceEntity.setType(type);
        deviceEntity.setDevicename("");
        deviceEntity.setDevicecode(MyTool.GUID());
        deviceEntity.setDeviceinfo(devicetag);
        deviceEntity.setStatus(0);
        deviceEntity.setCreate_time(MyTool.GenerateTime());
        deviceEntity.setDr(0);
        deviceEntity.setTenant_id(tenantId);
        deviceEntity.setYtenant_id(tenantId);
        deviceEntity.setOrgId("");
        deviceEntity.setVersion(2);
        deviceEntity.setApplicationId("");
        deviceEntity.setAppCode("");
        deviceEntity.setIpAddress("");
        deviceEntity.setPubts(MyTool.GenerateTime());
        deviceList.add(deviceEntity);
        deviceListMapper.InsertDevice(deviceList);
    }
}
